package nl.foldervisualizer.controller
{
	import flash.events.Event;
	import flash.filesystem.File;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.core.Application;
	import mx.events.CloseEvent;
	import mx.managers.CursorManager;
	
	import nl.foldervisualizer.business.FolderVisualizer;
	import nl.foldervisualizer.events.UIEvent;
	import nl.foldervisualizer.events.UIEventKind;
	import nl.foldervisualizer.model.Constants;
	import nl.foldervisualizer.model.Model;
	import nl.foldervisualizer.service.FileService;
	
	/**
	 * Application controller with generic application functionality.
	 */
	public class AppController extends MultiActionController
	{
		private var _fileService:FileService
		private var _visualizer:nl.foldervisualizer.business.FolderVisualizer
		private var _model:Model;
		private var previousView:int;
		
		public function set fileService(fileService:FileService):void {
			_fileService = fileService
		}
		
		public function set visualizer(visualizer:nl.foldervisualizer.business.FolderVisualizer):void {
			_visualizer = visualizer
		}
	
		public function set model(model:Model):void {
			_model = model
		}
	
		public function AppController()
		{
			super()
			super._handlerMappings.put(UIEventKind.SHUTDOWN, shutdown)
			super._handlerMappings.put(UIEventKind.OPEN, open)
			super._handlerMappings.put(UIEventKind.DELETE, handleDelete)
		}
		
		private function shutdown(event:UIEvent):void {
			Application.application.exit()
		}
		
		private function handleDelete(event:UIEvent):void {
			if (_model.selectedItem == null) {
				Alert.show("No file/folder selected!")
			} else if (Constants.OTHER == _model.selectedItem.path) {
				Alert.show("Aggregated folders cannot be deleted!")
			} else {
				Alert.show(_model.selectedItem.path, "Delete file/folder:",1|2,super.hostContainer,alertDeleteHandler)
			}
		}
		
		private function alertDeleteHandler(event:CloseEvent):void {
			if(event.detail == Alert.YES){
            	doDelete()
        	}
		}
		
		private function doDelete():void {
			var toDelete:File = new File(_model.selectedItem.path)
				trace("About to delete [" + toDelete.nativePath + "]")
				this._fileService.deleteFile(toDelete)
				_visualizer.removeFromFolderSizes(toDelete)
				_model.folderData = _visualizer.visualize(_model.folderToVisualize)
				_model.selectedItem = null
		}
		
		private function resetApp():void {
			_model.selectedItem = null
			_model.breadCrumps = new ArrayCollection()
			_model.folderData = new ArrayCollection()		
			_model.folderToVisualize = null	
		}
		
		public function open(event:UIEvent):void
		{
			var directory:File = new File()
		
			try {				
				this.previousView = _model.selectedView
				_model.selectedView = Constants.BUSY
				CursorManager.setBusyCursor()
				directory.addEventListener(Event.SELECT, directorySelected)
				directory.addEventListener(Event.CANCEL, cancelClicked)
				directory.browseForDirectory("Select a directory to visualize")
			} catch (error:Error) {
				Alert.show("Unable to open the selected directory", "Error")
			}
		}
		
		private function cancelClicked(event:Event):void {
			if (this.previousView != Constants.TREEMAP) {
				_model.selectedView = Constants.NOTHING_SELECTED	
			} else {
				_model.selectedView = Constants.TREEMAP
			}			
			CursorManager.removeBusyCursor()
		}

		private function directorySelected(event:Event):void {
			this.resetApp()
			_model.folderToVisualize = event.target as File		
			_visualizer.init(_model.folderToVisualize)
			_model.folderData = _visualizer.visualize(_model.folderToVisualize)		
			_model.selectedView = Constants.TREEMAP
			CursorManager.removeBusyCursor()
		}  
	}
}